//
//  SubjectDetailVC.m
//  Wedding fantastic
//
//  Created by dllo on 15/7/3.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SubjectDetailVC.h"
#import "MBProgressHUD.h"
#import "UIImageView+WebCache.h"
#import "RecommendModel.h"
#import "AFNetworking.h"
#import "SubjectDetailCell.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface SubjectDetailVC () <UICollectionViewDataSource, UICollectionViewDelegate>
// 小菊花
@property (nonatomic, retain) MBProgressHUD *HUD;

// 底部
@property (nonatomic, retain) UIScrollView *backScrollView;

// 背景imageView
@property (nonatomic, retain) UIImageView *backImage;

// 接收数据数组
@property (nonatomic, retain) NSMutableArray *dataArr;

//
@property (nonatomic, retain) UICollectionView *collectionView;
@end

@implementation SubjectDetailVC

- (void)dealloc
{
    [_sendModel release];
    [_HUD release];
    [_backScrollView release];
    [_backImage release];
    [_dataArr release];
    [_collectionView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:241 / 255.0 green:241 / 255.0 blue:240 / 255.0 alpha:1];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"主题详情";
    // 设置标题颜色
    self.navigationController.navigationBar.tintColor = [UIColor brownColor];
    // 字体颜色
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor brownColor],NSForegroundColorAttributeName,nil]];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png" ] style:UIBarButtonItemStyleDone target:self action:@selector(leftBack:)];
    
    // 背景图片
    self.backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [self.backImage sd_setImageWithURL:[NSURL URLWithString:self.sendModel.bkg_path]];
    [self.view addSubview:self.backImage];
    [_backImage release];
    
    // 创建底部scrollView
    self.backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64 - 49)];
    self.backScrollView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.backScrollView];
    
    // 按页滚动
    self.backScrollView.pagingEnabled = NO;
    // 隐藏滚动条
    self.backScrollView.showsHorizontalScrollIndicator = FALSE;
    
    [_backScrollView release];

    // 标题Label
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * WIDTH / 375, 330 * HEIGHT / 667, 250 * WIDTH / 375, 30 * HEIGHT / 667)];
    titleLabel.text = self.sendModel.title;
    titleLabel.textColor = [UIColor whiteColor];
    [self.backScrollView addSubview:titleLabel];
    [titleLabel release];
    
    // 简介label
    UILabel *introduceLabel = [[UILabel alloc] initWithFrame:CGRectMake(20 * WIDTH / 375, 370 * HEIGHT / 667, 250 * WIDTH / 375, 120 * HEIGHT / 667)];
    introduceLabel.text = self.sendModel.desc;
    introduceLabel.textColor = [UIColor whiteColor];
    introduceLabel.font = [UIFont systemFontOfSize:14];
    introduceLabel.numberOfLines = 0;
    [self.backScrollView addSubview:introduceLabel];
    [introduceLabel release];
    
    //
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(290 * WIDTH / 375, 450 * HEIGHT / 667);
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 80 * HEIGHT / 667, 20);
    //
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(300 * WIDTH / 375, 0, WIDTH * 17.8, 600 * HEIGHT / 667) collectionViewLayout:flowLayout];
    // collectionView和tableView很相似,使用前需要先签订两个协议,一个dataSource,一个delegate
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    [self.backScrollView addSubview:self.collectionView];
    [_collectionView release];
    
    // 注册
    // 第一个参数:需要指定注册类型
    // 第二个参数:重用的标志
    [self.collectionView registerClass:[SubjectDetailCell class] forCellWithReuseIdentifier:@"reuse"];
    [self createData];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SubjectDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    RecommendModel *model = self.dataArr[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    cell.layer.cornerRadius = 10;
    cell.layer.masksToBounds = YES;
    [cell.topImageView sd_setImageWithURL:[NSURL URLWithString:model.photo_path]];
    cell.nameLabel.text = model.subject_desc;
    return cell;
}

// 导航控制器返回按钮点击方法
- (void)leftBack:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createData
{
    self.dataArr = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:[NSString stringWithFormat:@"http://hunliji.com/subjects/%@.json?user_id=984695", self.sendModel.bId] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *productsArr = dic[@"products"];
        for (NSDictionary *tempDic in productsArr) {
            RecommendModel *model = [[RecommendModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.dataArr addObject:model];
            [model release];
        }
        // 滚动范围
        self.backScrollView.contentSize = CGSizeMake((WIDTH - 40) * self.dataArr.count, 0);
        
        [self.collectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
